﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SAM.Framework.MockData;

namespace SAM.MockData.Handlers
{
    public class MockHandlerForType<T> : IMockHandler<T>
    {

        public void Handler(MockContext<T> mockContext)
        {
            Type type = mockContext.Result.GetType();

            StringBuilder sb = new StringBuilder();
            string preKey = mockContext.ResultName;
            this.ConvertObjToString(preKey, sb, type);
            mockContext.CodeStr = mockContext.CodeStr + "; " + sb.ToString();
        }

        private void ConvertObjToString(string preKey, StringBuilder sb, Type type)
        {
            var props = type.GetProperties();
            foreach(var item in props)
            {
                var itemType = item.GetType();
                string tmp = preKey + "." + item.Name;
                if (itemType.IsPrimitive || typeof(string) == item.PropertyType )
                {
                    sb.Append(tmp + " = _" + tmp +"; ");
                }
                else
                {
                    ConvertObjToString(tmp, sb, itemType);
                }
            }
        }
    }
}
